 <html> 
 <head> 
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
 <title>js跨域访问</title> 
 <script type="text/JavaScript"> 
	 function sendIt(){ 
		 // 通过 postMessage 向子窗口发送数据
		 document.getElementById("otherPage").contentWindow 
			 .postMessage( 
				 document.getElementById("message").value, 
				"http://www.codeorigin.cn"//目标框架的域
			 ); 
	 };
	//接收子窗口的应答
    window.addEventListener("message", function(event) {
      	alert(event.origin + '说：' + event.data);
    }, false); 

 </script> 
 </head> 
 <body> 
	 <!-- 通过 iframe 嵌入子页面 --> 
	 <iframe src="http://www.codeorigin.cn/wp-content/themes/wp-dfrobot-po/demo/2016-11-1/postMessage.html" 
				 id="otherPage"></iframe> 
	 <br/><br/> 
	 <input type="text" id="message"><input type="button" 
			 value="Send to iframe" onclick="sendIt()" /> 
 </body> 
 </html>